<?php

$n = 0;
$this->Headers();
$ischanged = "0";
include ('class.ExtractMovieDetails.php');
// search only files of given types
$file_type = Array('asf', 'avi', 'dat', 'divx', 'flv', 'img', 'iso', 'm1v', 'm2p', 'm2t', 'm2ts', 'm2v', 'm4v', 'mkv', 'mov', 'mp4', 'mpg', 'mts', 'qt', 'rm', 'rmp4', 'rmvb', 'tp', 'trp', 'ts', 'vob', 'wmv');
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->Movies_Path, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $file => $fileinfo) {
    if (In_Array($fileinfo->getExtension(), $file_type) == true) {
        if (!preg_match("/\b(sample|cd2|part2)\b/i", $file)) { // if file contain specific words don't add it
            $results = $this->db->query("SELECT * FROM t1 WHERE path = '" . SQLite3::escapeString($file) . "'");
            while ($row = $results->fetchArray()) {
                $imdbidorig = $row['imdbid'];
                $nfoorigtime = $row['nfotime'];
                $oExMovDet = new ExtractMovieDetails($file, 0, 1);
                if (($imdbidorig != $oExMovDet->imdbid) || ($nfoorigtime != $oExMovDet->nfotime)) {
                    $ischanged = "1";
                    $n++;
                    $this->write_log($n . '. NFO file changes detected for ' . $file);
                    $oExMovDet = new ExtractMovieDetails($file, $ischanged);
                    $ischanged = "0";
                    $this->db->exec("UPDATE t1 SET imdbid = '$oExMovDet->imdbid', title = '$oExMovDet->title', genre = '$oExMovDet->genre', year = '$oExMovDet->year', plot = '$oExMovDet->plot', rating = '$oExMovDet->rating', cast = '$oExMovDet->cast', director = '$oExMovDet->director', runtime = '$oExMovDet->runtime', writer = '$oExMovDet->writer', location = '$oExMovDet->location', filetime = '$oExMovDet->filetime', nfotime = '$oExMovDet->nfotime', watched = '$oExMovDet->watched' WHERE path = '" . SQLite3::escapeString($file) . "'");
                    $this->write_log($n . '. Data added according to new NFO for file ' . $file);
                }
            }
            while (!$row = $results->fetchArray()) {
                $this->db->exec("INSERT OR REPLACE INTO t1 (path) SELECT '" . SQLite3::escapeString($file) . "' WHERE NOT EXISTS (SELECT 1 FROM t1 WHERE path = '" . SQLite3::escapeString($file) . "')");
                $n++;
                $this->write_log($n . '. New movie found. Searching imdb data...');
                $oExMovDet = new ExtractMovieDetails($file);
                $this->db->exec("UPDATE t1 SET imdbid = '$oExMovDet->imdbid', file = '" . SQLite3::escapeString($oExMovDet->filename) . "', ext = '$oExMovDet->ext', title = '$oExMovDet->title', path2 = '" . SQLite3::escapeString($oExMovDet->dirname) . "', genre = '$oExMovDet->genre', year = '$oExMovDet->year', plot = '$oExMovDet->plot', rating = '$oExMovDet->rating', cast = '$oExMovDet->cast', director = '$oExMovDet->director', runtime = '$oExMovDet->runtime', writer = '$oExMovDet->writer', location = '$oExMovDet->location', filetime = '$oExMovDet->filetime', nfotime = '$oExMovDet->nfotime', watched = '$oExMovDet->watched' WHERE path = '" . SQLite3::escapeString($file) . "'");
                $this->write_log($n . ". Added movie " . $file);
            }
        }
    }
}
// delete movies from database if file does not exists
$result = $this->db->query("SELECT path FROM t1");
while ($row = $result->fetchArray()) {
    if (!file_exists($row['path'])) {
        $rows = $row['path'];
        $rows = SQLite3::escapeString($rows);
        $this->db->exec("DELETE FROM t1 WHERE path = '".SQLite3::escapeString($rows)."'");
        $this->write_log('Mising movie deleted from database...');
    }
}
//$this->db->close();
// empty trashed info from database
$this->db->close();
$this->db = new SQLite3(self::DBPATH);
$this->db->exec("VACUUM");
$this->write_log('COMPLETE');
file_put_contents(SRJG::COMM, $n);
echo '<channel>';
$this->Footer();
?>
